#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define PATHNAME "/home/cjc/linux_code_test/linux_test_code/code10_20/shared_mamory"
#define SIZE 4096
#define PROJ_ID 0x6666

int main()
{
    //创建一个key值
    key_t key = ftok(PATHNAME,PROJ_ID);
    if(key < 0)
    {
        perror("ftok");
        return 1;
    }
    //创建一个共享内存
    int shm = shmget(key, SIZE, IPC_CREAT | IPC_EXCL);
    if(shm < 0)
    {
        perror("shmget");
        return 2;
    }

	printf("key: %x\n", key); //打印key值
	printf("shm: %d\n", shm); //打印句柄

    printf("attach begin!\n");
    sleep(2);
    char* mm = shmat(shm,NULL,0);
    if(mm == (void*)-1)
    {
        perror("shmat");
        return 3;
    }
    printf("attach end!\n");
    sleep(2);
    
    return 0;
}